package cn.yitun.chat.yitunserverimcore.general.main.push.chat;

import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.push.PushMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunservercommonlib.enums.control.push.room.RoomChatPushMethodEnum;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimcore.bridge.net.WriteHandler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 聊天室消息推送
 * @author: yitun
 * @date: 2016年8月26日 上午9:21:41
 */
@Service
public class RoomChatPush {
	//1.3.103
	final String action = ActionTypeConstant.ROOM_CHAT_ACTION_PUSH;

	@Resource
	WriteHandler writeHandler;

	/**
	 * 推送聊天室聊天信息
	 * 
	 * @author: yitun
	 * @createDate: 2016年11月29日 上午11:31:49
	 * @update: yitun
	 * @updateDate: 2016年11月29日 上午11:31:49
	 */
	public void pushRoomChat(Long key, Long roomId, Long userId, Content content, List<Long> userIdList) {
		PushMessage message = new PushMessage();
		message.bodyPut("roomId", roomId);
		message.bodyPut("userId", userId);
		message.bodyPut("content", content);

		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RoomChatPushMethodEnum.ROOM_CHAT_PUSH.getMethod());
		head.setKey(key);
		head.setTimestamp(System.currentTimeMillis());
		message.setHead(head);
		writeHandler.push(YitunStringUtil.toStrList(userIdList), message);
	}

	public void pushRoomChatInfo(Long roomId, UserVo userData, Content content, List<Long> userIdList) {
		PushMessage message = new PushMessage();
		message.bodyPut("roomId", roomId);
		message.bodyPut("userData", userData);
		message.bodyPut("content", content);

		HeadImpl head = new HeadImpl();
		head.setAction(action);
		head.setMethod(RoomChatPushMethodEnum.ROOM_CHAT_INFO_PUSH.getMethod());
		head.setKey(IdGen.getNumber());
		head.setTimestamp(System.currentTimeMillis());
		message.setHead(head);
		writeHandler.push(YitunStringUtil.toStrList(userIdList), message);
	}
}
